<?php
require_once('libraries/pagination.php');
require_once('frontend/models/categories.php');
$alias = $_GET['alias'];
//$category = get_a_record('categories', $id);
$category = categories_alias($alias);

if (!$category) {
    die('Error 404');
}
$id = $category['id'];

//phân trang
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$page = ($page>0) ? $page : 1;
$limit = 2;
$offset = ($page-1) * $limit;

$url = $category['alias'];

$options = array(
    'where' => 'category_id='.$id,
    'limit' => $limit,
    'offset' => $offset,
    'order_by' => 'id DESC'
);

$total_rows = get_total('articles', $options);
$total = ceil($total_rows / $limit);

//Để có trang tiếp theo
if($total_rows > ($page*$limit))
{
    $next = $page +1;
    $pagination2 = '<a href ="';
}

//dữ liệu
$title = $category['name'];
$articles = get_all('articles', $options);
$pagination = pagination($url, $page, $total, '?');
$categories = get_all('categories', array(
    'order_by' => 'position ASC'
));

//load view
$_content = 'frontend/views/articles/index.phtml';
require('frontend/views/common/layout.phtml');